import profile
from pathlib import Path

from django.db import models
from django.contrib.auth.models import User

from system.models import Role

app_path = Path(__file__).parent   # app的路径
app_static_path = app_path / 'static'   # 静态文件存放的位置


def user_head_img_path(obj,filename):
    """

    :param obj:
    :param filename: 文件名称
    :return: 文件保存的位置
    """
    return f"{app_static_path} / user_{obj.id} / {filename}"  # 绝对路径


class Profile(models.Model):
    object: models.QuerySet

    user = models.OneToOneField(User,on_delete=models.CASCADE)  # 与用户表建立 一对一的关系
    name = models.CharField("昵称",max_length=10)
    head_img = models.FileField("文件字段",upload_to=user_head_img_path)  # upload_to 文件上传的地址

    def get_role_list(self):
        role_list = Role.objects.filter(user=self.user)
        return role_list